import { useReactive } from "ahooks";
import { useModel } from "umi";
import compress from "gql-compress";

const useStaffResignated = () => {
    const { graphqlFetch } = useModel("global");

    const state = useReactive({
        loading: false,
    });

    const updateStaffResignated = async (staff, callback) => {
        state.loading = true;
        let variables = {
            osUuid: staff.osUuid,
            omUuid: staff.omUuid,
        };

        let query = staff.omUuid
            ? newQuery.replace("#changeMember", changeMember).replace("#omUuid", "$omUuid: UUID!")
            : newQuery.replace("#changeMember", "").replace("#omUuid", "");

        let res = await graphqlFetch({
            query,
            variables,
        });
        res && callback && callback(res);
        state.loading = false;
    };

    return { ...state, updateStaffResignated };
};

let newQuery = compress(`mutation MyMutation(
    $osUuid: UUID!
    #omUuid
  ) {
    updateOrganizationStaffByOsUuid(
        input: {
            osUuid: $osUuid
            organizationStaffPatch: {
                osStat: false
                osType: "离职"
            }
        }
    ) {
        clientMutationId
    }

    #changeMember
}`);

const changeMember = `member: updateOrganizationMemberByOmUuid(
    input: {
        omUuid: $omUuid
        organizationMemberPatch: {
            omIfim: false
            omAust: "已离职"
        }
    }
) {
    clientMutationId
}`;

export default useStaffResignated;
